package com.jy.servlet;

import com.google.gson.Gson;
import com.jy.dao.ManageDao;
import com.jy.dao.UserDao;
import com.jy.dao.impl.ManageDaoImpl;
import com.jy.dao.impl.UserDaoImpl;
import com.jy.entity.*;
import com.jy.util.CosFileupload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/stu/*")
public class StudentServlet extends BaseServlet {
	UserDao user=new UserDaoImpl();
	//获取试卷
	protected void gettimetest(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Object sf=request.getSession().getAttribute("sf");
		Object act=request.getSession().getAttribute("act");
		Student s1=user.getstuid((String)act);
		if(sf=="student") {
			String sql="select * from exam where exam_type=? and exam_zt=?";
			List list=user.gettimetest(sql,"时段试卷",1);
			List list1=user.gettimetest("select * from exam where exam_type=? and exam_zt=? and exam_classid=?","班级试卷",1,s1.getClassid());
			List list2=user.gettimetest(sql,"固定试卷",1);
			request.setAttribute("list",list);
			request.setAttribute("list1",list1);
			request.setAttribute("list2",list2);
			request.getRequestDispatcher("/html/testcenter.jsp").forward(request, response);
		}else if(sf=="teacher"){
			String sql="select * from exam where exam_type=? and exam_creator_id=?";
			Object tid=request.getSession().getAttribute("tid");
			List list=user.gettimetest(sql,"时段试卷",tid);
			List list1=user.gettimetest(sql,"班级试卷",tid);
			List list2=user.gettimetest(sql,"固定试卷",tid);
			request.setAttribute("list",list);
			request.setAttribute("list1",list1);
			request.setAttribute("list2",list2);
			request.getRequestDispatcher("/html/teachertestcenter.jsp").forward(request, response);
		}
	}

	Gson gson=new Gson();
	//查班级
	protected void cls(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//	response.setContentType("text/html");
		response.setContentType("application/json");
		String sql="select *from classes";
		try {
			List c=user.getclasses(sql);
			System.out.println(c);
			String reg=gson.toJson(c);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//获取视频
	protected void getvideo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String sql="select * from video where vtype=?";
		String sql1="select *from video where vtype=? and classid=?";
		Object name=request.getSession().getAttribute("act");
		Object sf=request.getSession().getAttribute("sf");
		if(sf=="student") {
			Student s=user.getstuid((String)name);
			List list1=user.getvideo(sql,"教学视频");
			List list2=user.getvideo(sql1,"课堂回放",s.getClassid());
			request.setAttribute("list1",list1);
			request.setAttribute("list2",list2);
			request.getRequestDispatcher("/html/videoclass.jsp").forward(request, response);
		}else if(sf=="teacher"){
			Teacher t=user.getteacher("select *from teacher where tname=?",name);
			List list1=user.getvideo("select *from video where vtype=? and teacherid=?","教学视频",t.getTeacherId());
			List list2=user.getvideo("select *from video where vtype=? and teacherid=?","课堂回放",t.getTeacherId());
			request.setAttribute("list1",list1);
			request.setAttribute("list2",list2);
			request.getRequestDispatcher("/html/teachervideo.jsp").forward(request, response);
		}
	}


	//查询班级
	protected void findclass(HttpServletRequest request, HttpServletResponse response) throws IOException {
		UserDao user = new UserDaoImpl();
		Object name=request.getSession().getAttribute("act");
		try {
			Student s=user.getstuid((String) name);
			System.out.println(s.toString());
			List list1=user.findclass(s.getClassid());
			Classes c =user.classmsg(s.getClassid());
			System.out.println("登陆用户ID:"+s.getClassid());
			request.getSession().setAttribute("list",list1);
			request.getSession().setAttribute("bzr",c.getBzr());
			request.getSession().setAttribute("java",c.getJava());
			request.getSession().setAttribute("class",c.getClassRemark());
			request.getSession().setAttribute("cname",c.getClassName());
			request.getSession().setAttribute("classtime",c.getClassCreate());
			response.sendRedirect("/examproject/html/myclass.jsp");
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}

	//查询个人信息
	protected void myinfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		UserDao user = new UserDaoImpl();
		Object name=request.getSession().getAttribute("act");
		Object sf=request.getSession().getAttribute("sf");
		if(sf=="student") {
			List list = user.getyh((String) name);
			System.out.println(list);
			Student stu = user.getstuid((String) name);
			//通过班级id查班级
			Classes cls=user.classmsg(stu.getClassid());
			System.out.println(cls);
			request.getSession().setAttribute("cls",cls.getClassName());
			request.getSession().setAttribute("stulist",list);
			response.sendRedirect("/examproject/html/myInfo.jsp");
		}else{
			String sql="select *from teacher";
			List list=user.getls((String)name);
			System.out.println(list);
			request.getSession().setAttribute("stulist",list);
			response.sendRedirect("/examproject/html/myInfo.jsp");
		}

	}

	//修改个人信息
	protected void updateinfo(HttpServletRequest request, HttpServletResponse response) throws Exception{
		UserDao user = new UserDaoImpl();
		Object sf=request.getSession().getAttribute("sf");
		Object name=request.getSession().getAttribute("act");
		Student stu=new Student();
		Teacher te=new Teacher();
		if(sf=="student") {
			DiskFileItemFactory factory=new DiskFileItemFactory();
			//2
			ServletFileUpload upload=new ServletFileUpload(factory);

			upload.setHeaderEncoding("UTF-8");
			try {
				//转换对象 返回list集合是怕上传多个文件
				List<FileItem> list=upload.parseRequest(request);
				//存盘
				for(FileItem fi:list){
					//判断是否普通文本框  否则是文件
					if(fi.isFormField()){
						String gefn=fi.getFieldName();
						String val=fi.getString("UTF-8");
						switch(gefn){
							case "tel":
								stu.setStphone(val);
								break;
							case "pwd":
								stu.setStpass(val);
								break;
							case "sex":
								stu.setStsex(val);
								break;
							case "wx":
								stu.setStwx(val);
								break;
						}

					}else{
						if(fi.getName()==null || fi.getName().equals("") || fi.getName()=="" || fi.getName().isEmpty()){
							Student stlist = user.getstuid((String) name);
							String img=stlist.getStavatar();
							stu.setStname((String) name);
							stu.setStavatar(img);
							int n = user.updatestuinfo(stu);
							if(n>0){
								System.out.println("修改成功");
							}else{
								System.out.println("修改失败");
							}
							myinfo(request,response);
						}else{
							if(fi.getName().endsWith(".jpg") || fi.getName().endsWith(".png")) {
								long ti=System.currentTimeMillis();
								int wz=fi.getName().lastIndexOf(".");
								String pricn=fi.getName().substring(wz+1);
								System.out.println("后缀名："+pricn);
								String pna=ti+"."+pricn;
								System.out.println("图片名："+pna);
								InputStream in=fi.getInputStream();
								CosFileupload.upfile(in,pna);
								stu.setStavatar("https://sls-cloudfunction-ap-guangzhou-code-1301165591.cos.ap-guangzhou.myqcloud.com/"+pna);
								in.close();
							}else {
								response.setContentType("text/html;charset=UTF-8");
								PrintWriter out = response.getWriter();
								out.print("<script>alert('请上传后缀名为jpg、png的图片文件！');location.href='/examproject/html/updateInfo.jsp'</script>");
							}
						}
					}
				}
			} catch (FileUploadException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			stu.setStname((String) name);
			int n = user.updatestuinfo(stu);
			if(n>0){
				System.out.println("修改成功");
			}else{
				System.out.println("修改失败");
			}
			myinfo(request,response);
		}else{

			DiskFileItemFactory factory=new DiskFileItemFactory();
			//2
			ServletFileUpload upload=new ServletFileUpload(factory);

			upload.setHeaderEncoding("UTF-8");

			//转换对象 返回list集合是怕上传多个文件
			List<FileItem> list=upload.parseRequest(request);

			for(FileItem fi:list){
				if(fi.isFormField()){
					String gefn=fi.getFieldName();
					String val=fi.getString("UTF-8");
					switch (gefn){
						case "tel":
							te.setTphone(val);
							System.out.println("tel："+val);
							break;
						case "pwd":
							te.setTpass(val);
							System.out.println("pwd："+val);
							break;
					}

				}else{
					if(fi.getName()==null || fi.getName().equals("") || fi.getName()=="" || fi.getName().isEmpty()){
						Teacher tlis=user.getteacher("select * from teacher where tname=?",(String)name);
						String img=tlis.getThavatar();
						te.setTname((String) name);
						te.setThavatar(img);
						int n=user.updateteainfo(te);
						if(n>0){
							System.out.println("修改成功");
						}else{
							System.out.println("修改失败");
						}
						myinfo(request,response);
					}else{
						if(fi.getName().endsWith(".jpg") || fi.getName().endsWith(".png")) {
							long ti=System.currentTimeMillis();
							int wz=fi.getName().lastIndexOf(".");
							String pricn=fi.getName().substring(wz+1);
							/*	System.out.println("后缀名："+pricn);*/
							String pna=ti+"."+pricn;
							//System.out.println("图片名："+pna);
							InputStream in=fi.getInputStream();
							CosFileupload.upfile(in,pna);
							te.setThavatar("https://sls-cloudfunction-ap-guangzhou-code-1301165591.cos.ap-guangzhou.myqcloud.com/"+pna);
							in.close();
						}else {
							response.setContentType("text/html;charset=UTF-8");
							PrintWriter out = response.getWriter();
							out.print("<script>alert('请上传后缀名为jpg、png的图片文件！');location.href='/examproject/html/updateInfo.jsp'</script>");
						}
					}
				}
			}
			te.setTname((String) name);
			System.out.println("修改后的信息:" + te);
			int n=user.updateteainfo(te);
			if(n>0){
				System.out.println("修改成功");
			}else{
				System.out.println("修改失败");
			}
			myinfo(request,response);
			return;
		}
	}
	//得到试卷题目
	protected void gettestcontent(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("exid");
		System.out.println(id);
		String sql = "select *from exam where exam_id=?";
		int exmaid = Integer.parseInt(id);
		System.out.println(exmaid);
		Object name = request.getSession().getAttribute("act");
		Student s1 = user.getstuid((String) name);
		List ksjl = user.getexamrecord("select *from exam_record where user_id=? and exam_id=?", s1.getStid(),exmaid);
		Timetest time = user.getTimetest(sql, exmaid);
		System.out.println("考试记录:"+ksjl);
		System.out.println("size："+ksjl.size());

		if(ksjl.size()==0) {
			ExamRecord e = new ExamRecord();
			e.setExam_id(exmaid);
			e.setExam_zt("待交卷");
			e.setUser_id(s1.getStid());
			e.setExam_type(time.getExam_type());
			e.setExam_name(time.getExam_name());
			e.setExam_limit_time(time.getExam_time_limit());
			int ex = user.addexamrecord(e);
			if (ex > 0) {
				System.out.println("添加成功");
			} else {
				System.out.println("添加失败");
			}
		}
		//单选总分
		int dsum = user.secount(exmaid, 1);
		List list1 = user.gettimetest(sql, exmaid);
		List xlist = user.seexid(exmaid, 1);
		List dlist = user.seexid(exmaid, 2);
		int dxnum=user.secount(exmaid,2);
		System.out.println("总分:" + dsum);
		System.out.println(xlist);
		System.out.println(dlist);
		System.out.println(list1);
		request.setAttribute("list1", list1);
		request.setAttribute("xlist", xlist);
		request.setAttribute("dlist", dlist);
		request.setAttribute("dsum", dsum);
		request.setAttribute("dxnum",dxnum);
		request.setAttribute("ksid",exmaid);
		request.getRequestDispatcher("/html/finalexam.jsp").forward(request, response);
	}
	//删除考试记录
	protected void delrecords(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("exid");
		System.out.println(id);
		int exmaid = Integer.parseInt(id);
		System.out.println(exmaid);
		ManageDao m=new ManageDaoImpl();
		int n=m.delsj(exmaid);
		if(n>0){
			System.out.println("删除成功");
		}
		response.sendRedirect("/examproject/stu/getexamrecord");
	}
	//创建考试
	protected void createtest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Object tid1=request.getSession().getAttribute("tid");
		int tid=(int)tid1;
		Date now=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Timetest t=new Timetest();
		t.setCreate_time(now);
		t.setExam_zt(2);
		t.setExam_creator_id(tid);
			DiskFileItemFactory factory=new DiskFileItemFactory();
			//2
			ServletFileUpload upload=new ServletFileUpload(factory);
			upload.setHeaderEncoding("UTF-8");
			//转换对象 返回list集合是怕上传多个文件
			List<FileItem> list=upload.parseRequest(request);
			for(FileItem fi:list){
				if(fi.isFormField()){
					String gefn=fi.getFieldName();
					String val=fi.getString("UTF-8");
					switch (gefn){
						case "exam_name":
							t.setExam_name(val);
							System.out.println("name："+val);
							break;
						case "exam_desc":
							t.setExam_desc(val);
							System.out.println("desc："+val);
							break;
						case "exam_class":
							t.setExam_classid(Integer.parseInt(val));
							System.out.println("班级id："+val);
							break;
						case "exam_type":
							t.setExam_type(val);
							System.out.println("type："+val);
							break;
						case "exam_time":
							t.setExam_time_limit(Integer.parseInt(val));
							System.out.println("time："+val);
							break;
						case "exam_begin":
							Date begin=sdf.parse(val);
							t.setExam_start_date(begin);
							System.out.println("begin："+begin);
							break;
						case "exam_end":
							Date end=sdf.parse(val);
							t.setExam_end_date(end);
							System.out.println("end："+end);
							break;
					}
				}else{
					if(fi.getName()==null || fi.getName().equals("") || fi.getName()=="" || fi.getName().isEmpty()){
						System.out.println("没有文件:"+t);
							int n = user.addtest(t);
							if (n > 0) {
								System.out.println("试卷创建成功!!!");
							} else {
								System.out.println("创建失败");
							}
						response.sendRedirect("/examproject/stu/gettimetest");
					}else{
						if(fi.getName().endsWith(".jpg") || fi.getName().endsWith(".png")) {
							long ti=System.currentTimeMillis();
							int wz=fi.getName().lastIndexOf(".");
							String pricn=fi.getName().substring(wz+1);
							/*	System.out.println("后缀名："+pricn);*/
							String pna=ti+"."+pricn;
							//System.out.println("图片名："+pna);
							InputStream in=fi.getInputStream();
							CosFileupload.upfile(in,pna);
							System.out.println("图片:"+pna);
							t.setExam_avatar("https://sls-cloudfunction-ap-guangzhou-code-1301165591.cos.ap-guangzhou.myqcloud.com/"+pna);
							System.out.println("有图片:"+t);
							int n=user.addtest(t);
							if(n>0){
								System.out.println("试卷创建成功!!!");
							}else{
								System.out.println("创建失败");
							}
							response.sendRedirect("/examproject/stu/gettimetest");
							in.close();
						}else {
							response.setContentType("text/html;charset=UTF-8");
							PrintWriter out = response.getWriter();
							out.print("<script>alert('请上传后缀名为jpg、png的图片文件！');location.href='/examproject/stu/gettimetest'</script>");
						}
					}
				}
			}
	}

	//创建视频
	protected void createvideo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Object tid1=request.getSession().getAttribute("tid");
		int tid=(int)tid1;
		Date now=new Date();
		Video v=new Video();
		v.setVtime(now);
		v.setTeacherid(tid);
		DiskFileItemFactory factory=new DiskFileItemFactory();
		//2
		ServletFileUpload upload=new ServletFileUpload(factory);
		upload.setHeaderEncoding("UTF-8");
		//转换对象 返回list集合是怕上传多个文件
		List<FileItem> list=upload.parseRequest(request);
		for(FileItem fi:list){
			if(fi.isFormField()){
				String gefn=fi.getFieldName();
				String val=fi.getString("UTF-8");
				switch (gefn){
					case "vname":
						v.setVname(val);
						System.out.println("name："+val);
						break;
					case "vdesc":
						v.setVdesc(val);
						System.out.println("desc："+val);
						break;
					case "classid":
						v.setClassid(Integer.parseInt(val));
						System.out.println("班级id："+val);
						break;
					case "vtype":
						v.setVtype(val);
						System.out.println("视频类型："+val);
						break;
				}
			}else{
				if(fi.getName()==null || fi.getName().equals("") || fi.getName()=="" || fi.getName().isEmpty()){
					System.out.println("没有文件:"+v);
					int n = user.addvideo(v);
					if (n > 0) {
						System.out.println("视频发布成功!!!");
					} else {
						System.out.println("发布失败");
					}
					response.sendRedirect("/examproject/stu/getvideo");
				}else{
					if(fi.getName().endsWith(".mp4") || fi.getName().endsWith(".mkv")) {
						long ti=System.currentTimeMillis();
						int wz=fi.getName().lastIndexOf(".");
						String pricn=fi.getName().substring(wz+1);
						/*	System.out.println("后缀名："+pricn);*/
						String pna=ti+"."+pricn;
						//System.out.println("视频名："+pna);
						InputStream in=fi.getInputStream();
						CosFileupload.upfile(in,pna);
						System.out.println("视频:"+pna);
						v.setVurl("https://sls-cloudfunction-ap-guangzhou-code-1301165591.cos.ap-guangzhou.myqcloud.com/"+pna);
						System.out.println("有视频:"+v);

						int n=user.addvideo(v);
						if(n>0){
							System.out.println("视频发布成功!!!");
						}else{
							System.out.println("视频发布失败");
						}
						response.sendRedirect("/examproject/stu/getvideo");

						in.close();
					}else {
						response.setContentType("text/html;charset=UTF-8");
						PrintWriter out = response.getWriter();
						out.print("<script>alert('请上传后缀名为mp4、mkv的视频文件！');location.href='/examproject/stu/getvideo'</script>");
					}
				}
			}
		}
	}
	//得到题目
	protected void question(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("application/json");
		String name1=request.getParameter("name");
		String name = new String(name1.getBytes("ISO-8859-1") , "UTF-8");
		System.out.println(name);
		if(name.equals("所有试题")){
			String sql="select *from question";
			List list=user.gettestquestion(sql);
			System.out.println(list);
			String reg=gson.toJson(list);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		}else {
			book b = user.getbook2("select *from book where book_name=?", name);
			String sql = "select *from question where question_bookid=?";
				List list = user.gettestquestion(sql, b.getBook_id());
				System.out.println(list);
				String reg = gson.toJson(list);
				PrintWriter out = response.getWriter();
				out.print(reg);
				out.flush();
				out.close();

		}
	}


	//添加题目到试卷中
	protected void addquestion(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String question_score=request.getParameter("question_score");
		String question_ids=request.getParameter("question_ids");
		String id=request.getParameter("id");
		System.out.println("分数:"+question_score);
		System.out.println("ids:"+question_ids);
		System.out.println("试卷id:"+id);
		Timetest t1=new Timetest();
		t1.setExam_question_ids(question_ids);
		t1.setExam_score(Integer.parseInt(question_score));
		t1.setExam_zt(1);
		t1.setExam_id(Integer.parseInt(id));
		System.out.println("修改后试卷:"+t1);
		int count=user.updatetest(t1);
		if(count>0){
			System.out.println("题目导入成功");
		}else{
			System.out.println("导入失败");
		}
		gettimetest(request,response);
	}

	//判断是否参加了该考试
	protected void pdexam(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Object name=request.getSession().getAttribute("act");
		Student s1=user.getstuid((String)name);
		String id=request.getParameter("exam_id");
		System.out.println("试卷id:"+id);
		response.setContentType("application/json");
		try {
			List ksjl=user.gettimetest("select *from exam_record where user_id=? and exam_id=?",s1.getStid(),Integer.parseInt(id));
			System.out.println(ksjl);
			String reg=gson.toJson(ksjl);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//获取当前时间
	protected void getnowtime(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("application/json");
			Date now=new Date();
			System.out.println(now);
			String reg=gson.toJson(now);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
	}
	//得到book类别
	protected void book(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("application/json");
		String sql="select *from book";
		try {
			List book=user.getbook(sql);
			System.out.println(book);
			String reg=gson.toJson(book);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//模拟测试

	public void test(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {
                String name = request.getParameter("name");
                String danx2 = request.getParameter("danx");
                int bid=Integer.parseInt(name);
				System.out.println("分类id："+bid);
                int danx = Integer.parseInt(danx2);
                System.out.println(danx);
                List<Question> danxlist = user.getdan(bid,danx);
                request.getSession().setAttribute("danxlist",danxlist);
				response.sendRedirect("/examproject/html/mncsm.jsp");
	}
	//得到模拟试卷
	protected void getmnsj(HttpServletRequest request, HttpServletResponse response) throws Exception{
		response.setContentType("application/json");
		Object danx=request.getSession().getAttribute("danxlist");
			System.out.println("得到模拟题:"+danx);
			String reg=gson.toJson(danx);
			PrintWriter out=response.getWriter();
			out.print(reg);
			out.flush();
			out.close();
	}

	//获取当前用户考试记录
	protected void getexamrecord(HttpServletRequest request, HttpServletResponse response) throws Exception{
		Object name=request.getSession().getAttribute("act");
		System.out.println(name);
		Student s=user.getstuid((String)name);
		List examrecord=user.getexamrecord("select *from exam_record where user_id=?",s.getStid());
		System.out.println(examrecord);
		request.setAttribute("examrecord",examrecord);
		request.getRequestDispatcher("/html/testrecords.jsp").forward(request, response);
	}

	/**
	 * 添加评论
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void addcomments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int stuid = Integer.parseInt(request.getSession().getAttribute("stuid").toString());
		String comment = request.getParameter("comment");
		//System.out.println(uid+"添加评论！"+comment);
		SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = simp.format(new Date());
		//System.out.println(time);
		Comment con = new Comment(stuid,comment,time);
		user.addComment(con);
		request.getRequestDispatcher("/stu/queryJ").forward(request, response);
	}

	/**
	 * 添加留言
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void addreply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int stuid = Integer.parseInt(request.getSession().getAttribute("stuid").toString());
		int pid = Integer.parseInt(request.getParameter("pid"));
		int type = Integer.parseInt(request.getParameter("htype"));
		int cid = Integer.parseInt(request.getParameter("cid"));
		String comment = request.getParameter("comment2");
		SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = simp.format(new Date());

		Reply reply = new Reply(stuid,pid,type,cid,comment,time);
		user.addreply(reply);
		request.getRequestDispatcher("/stu/queryJ").forward(request, response);
	}

	/**
	 * 查询评论
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void queryJ(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		int stuid = Integer.parseInt(request.getSession().getAttribute("stuid").toString());
		Student stu = user.getStudent(stuid);
		request.setAttribute("students",stu);         //学生信息

		List stulist = user.getStudentList();
		List list = user.querypl();
		request.setAttribute("studentsall",stulist);  //所有学生
		request.setAttribute("comments",list);        //所有评论信息

		List replist = user.queryly();
		request.setAttribute("replist",replist);      //所有留言信息


		request.getRequestDispatcher("/html/message.jsp").forward(request,response);

	}

	//---------------------------------------作业情况
   protected void addhom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   String ctime=request.getParameter("ctime");
	   String bx=request.getParameter("bx");
	   if(ctime==null){
		   ctime="01";
	   }
	   System.out.println("bx就是"+bx);
	   HttpSession session=request.getSession();
	   System.out.println("ctime的长度"+ctime.length());
	   System.out.println(ctime.toString().length()+"+++");
	   UserDao user=new UserDaoImpl();
	   try {
		   if(ctime.length()>5){

			   List c=(List) session.getAttribute("c");
			   List list=user.addhom(ctime.substring(5,7));
			   System.out.println(list);

			   session.setAttribute("list",list);
			   session.setAttribute("bx",bx);

			   response.sendRedirect("/examproject/html/zyqk.jsp");
		   }else{

			   List list=user.addhom(ctime);
			   System.out.println(list);
			   session.setAttribute("list",list);
			   session.setAttribute("bx",bx);
			   response.sendRedirect("/examproject/html/zyqk.jsp");
		   }
	   } catch (SQLException throwables) {
		   throwables.printStackTrace();
	   }
    }

    protected void tjhom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String ctime=request.getParameter("ctime");
		String stname=request.getParameter("stname");
		HttpSession session=request.getSession();
		ManageDao us=new ManageDaoImpl();
		try {
			List c=us.findstu2(stname);
			session.setAttribute("c",c);
			session.setAttribute("ctime",ctime);
			System.out.println(c);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
		request.getRequestDispatcher("/stu/addhom").forward(request,response);
    }

    protected void addid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		HttpSession session=request.getSession();
		String Stname=request.getParameter("stname");
		System.out.println("姓名:"+Stname);
		String checkIn=request.getParameter("checkIn");
		String work=request.getParameter("work");
		String ctime=request.getParameter("ctime");
		String remark=request.getParameter("remark");
		try {
			UserDao use=new UserDaoImpl();
			List<Object> list=new ArrayList<Object>(use.addid(Stname));
			System.out.println(list);
			System.out.println("list.Array().length的长度"+list.toArray().length);
			Object[] array = new Object[list.size()];
			if(list.toArray().length==0){
				System.out.println("进来了");
				request.getRequestDispatcher("/stu/addhom?bx=false&ctime=01").forward(request,response);
			}
			System.out.println("可以运行到这里");
			array[0] = list.get(0);
			session.setAttribute("stname",Stname);
			session.setAttribute("checkIn",checkIn);
			session.setAttribute("work",work);
			session.setAttribute("ctime",ctime);
			session.setAttribute("remark",remark);
			session.setAttribute("array",array[0]);
			request.getRequestDispatcher("/stu/tjzy").forward(request,response);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}

    }
    protected void tjzy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao user = new UserDaoImpl();
		HomeWork h=new HomeWork();
		HttpSession session=request.getSession();
		String checkIn=request.getParameter("checkIn");
		String work=request.getParameter("work");
		String ctime=request.getParameter("ctime");
		String remark=request.getParameter("remark");
		Object sid=session.getAttribute("array");

		System.out.println("这是array=="+sid);
		System.out.println("这是checkIn=="+checkIn);
		System.out.println("这是work=="+work);
		System.out.println("这是ctime=="+ctime);
		System.out.println("这是remark=="+remark);
		String sid1=sid.toString().substring(sid.toString().indexOf("=")+1,sid.toString().length());
		System.out.println("sid1=="+sid1);
		h.setStudentId(Integer.parseInt(sid1));
		h.setCheckIn(checkIn);
		h.setWork(work);
		h.setCtime(ctime);
		h.setRemark(remark);
		try {
			int c=user.tjzy(h);
			System.out.println(c);
			session.setAttribute("ctime",ctime);
			request.getRequestDispatcher("/stu/tjhom").forward(request,response);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
    }
    //删除
    protected void delhom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDao user = new UserDaoImpl();
		HomeWork h=new HomeWork();
		String id=request.getParameter("id");
		String ctime=request.getParameter("ctime");
		System.out.println(ctime);
		try {
			int c=user.delhom(Integer.parseInt(id));
			System.out.println(c);
			response.sendRedirect("addhom?ctime="+ctime);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
    }

    //查询错题本
	protected void falsebook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object stuid=request.getSession().getAttribute("stuid");
		System.out.println("用户id:"+stuid);
		int id=(int)stuid;
		try {
			List list1=user.falsebook("select *from falsebook where user_id=? and exam_type=?",id,"单选");
			List list2=user.falsebook("select *from falsebook where user_id=? and exam_type=?",id,"多选");
			request.setAttribute("list1",list1);
			request.setAttribute("list2",list2);
			request.getRequestDispatcher("/html/falsebook.jsp").forward(request,response);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}

	//查询试卷解析
	protected void detailpage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Object stuid=request.getSession().getAttribute("stuid");
		String exid1=request.getParameter("exid");
		int exid=Integer.parseInt(exid1);
		System.out.println("用户id:"+stuid);
		int id=(int)stuid;
		try {
			List list1=user.detailpage("select *from detailpage where user_id=? and exam_id=? and exam_type=?",id,exid,"单选");
			List list2=user.detailpage("select *from detailpage where user_id=? and exam_id=? and exam_type=?",id,exid,"多选");
			Object dsum=user.detaildsum(exid,id,"单选");
			Object dxnum=user.detaildsum(exid,id,"多选");
			request.setAttribute("xlist",list1);
			request.setAttribute("dlist",list2);
			request.setAttribute("dsum",dsum);
			request.setAttribute("dxnum",dxnum);
			request.getRequestDispatcher("/html/Examdetailpage.jsp").forward(request,response);
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}
	}

}
